package org.wlw.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class TestStatus {
	public static void main(String[] args) {
		File f = new File("d:/teach_source/21_wlw/");
//		System.out.println(getLineNum(f));
		System.out.println(getLineByFile(f,".java"));
	}
	
	public static int getLineByFile(File f,String type) {
		int num = 0;
		if(f.isDirectory()) {
			//如果是目录，应该列表所以的文件，文件就从文件里面找
			File[] fs = f.listFiles();
			for(File file:fs) {
				num+=getLineByFile(file,type);
			}
		} else {
			if(f.getName().endsWith(type)) {
				num+=getLineNum(f);
			}
		}
		return num;
	}
	
	public static int getLineNum(File f) {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(f));
			String s = null;
			int num = 0;
			while((s=br.readLine())!=null) {
				num+=1;
			}
			return num;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(br!=null) br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}
}
